package com.cimu.redis.service.impl;

import com.cimu.redis.entity.User;
import com.cimu.redis.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Title: UserServiceImpl
 * Copyright: Copyright (c) 2017
 *
 * date 2018年11月28日 12:13
 */
@Service
public class UserServiceImpl implements UserService{

    @Cacheable(value = "getById",cacheManager = "fiveMinutesManager", key = "#id" )
    @Override
    public User getById(Long id) {
        //这里模拟获取用户数据  实际从数据库获取
        return User.builder().id(1L).mobile("18888888888").build();
    }

    @Cacheable(value = "selectAll")
    @Override
    public List<User> selectAll() {
        List<User> users = new ArrayList<>();
        users.add(User.builder().id(1L).mobile("18888888888").build());
        users.add(User.builder().id(2L).mobile("18888888887").build());
        return users;
    }

    @CacheEvict(value = "getById", allEntries = true)//删除一个缓存key
//    @CacheEvict(value = {"getById","selectAll"}, allEntries = true)//删除多个缓存key
    @Override
    public void saveOrUpdate(User user) {
        //模拟修改
    }

}
